package ocean

import (
	"context"
	"fmt"
	oceanSdk "github.com/oceanengine/ad_open_sdk_go"
	"github.com/oceanengine/ad_open_sdk_go/config"
	. "github.com/oceanengine/ad_open_sdk_go/models"
)

type OceanClient struct {
	cli          *oceanSdk.Client
	accessToken  string
	advertiserID int64
}

func NewOceanClient(accessToken string, advertiserID int64) *OceanClient {
	cfg := config.NewConfiguration()
	cfg.AddDefaultHeader("Access-Token", accessToken)

	configuration := config.NewConfiguration()
	apiClient := oceanSdk.Init(configuration)
	apiClient.SetLogEnable(true)
	return &OceanClient{
		cli:          apiClient,
		accessToken:  accessToken,
		advertiserID: advertiserID,
	}
}

// CreateProject 创建项目（对应ProjectCreateV30Request）
func (c *OceanClient) CreateProject(ctx context.Context, req ProjectCreateV30Request) (int64, error) {

	resp, httpResp, err := c.cli.ProjectCreateV30Api().
		Post(ctx).
		ProjectCreateV30Request(req).
		Execute()
	if err != nil {
		return 0, fmt.Errorf("API error: %v", err)
	}
	if httpResp.StatusCode != 200 || resp.Data.ProjectId == nil {
		return 0, fmt.Errorf("create project failed: code=%d, msg=%s",
			resp.Code, resp.Message)
	}
	return *resp.Data.ProjectId, nil
}

// CreatePromotion 创建广告（对应PromotionCreateV30Request）
func (c *OceanClient) CreatePromotion(ctx context.Context, req PromotionCreateV30Request) (int64, error) {
	resp, httpResp, err := c.cli.PromotionCreateV30Api().
		Post(ctx).
		PromotionCreateV30Request(req).
		Execute()
	if err != nil {
		return 0, fmt.Errorf("API error: %v", err)
	}
	if httpResp.StatusCode != 200 || resp.Data.PromotionId == nil {
		return 0, fmt.Errorf("create project failed: code=%d, msg=%s",
			resp.Code, resp.Message)
	}
	return *resp.Data.PromotionId, nil
}
